package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.domain.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : leimingchao
 * @date Date : 2022年02月16日 0:48
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private StorageService storageService;

    @Autowired
    private AccountService accountService;


    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
    public void create(Order order) {

        log.info("-------->开始新建订单");
        //1.新建订单
        orderDao.create(order);

        log.info("-------->订单微服务开始调用库存，做扣减count");
        //2.扣减库存
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("-------->订单微服务开始调用库存，做扣减end");

        log.info("-------->订单微服务开始调用账户，做扣减money");
        //3.扣减余额
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("-------->订单微服务开始调用账户，做扣减end");

        //4.修改订单状态，从0------>1,1代表已经完成
        log.info("-------->修改订单状态开始");
        orderDao.update(order.getUserId(), 0);
        log.info("-------->修改订单状态结束");

        log.info("-------->下订单结束，O(∩_∩)O哈哈~");
    }
}
